package pl.netcoffee.android.finserDroid;


import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class finserDroid extends ListActivity {
	
	static final int DIALOG_MANUAL_COMMAND = 1;
	
	
	
	AppLogic al = null;
	ServiceProvider api = null;
	
	private Dialog dialogManualCmd;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        al = AppLogic.getInstance(this);
        api = al.getServiceProvider();
        
        String[] menuStrings = new String[]{"Płatność gotówką", "Własna komenda", "Płatność kartą", "Wypłata z bankomatu"};
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuStrings));
        
    }
    
    protected void onListItemClick(ListView l, View v, int position, long id){
    	
    	if(position == 0){
    		
    		Intent i = new Intent(this, RegisterCashPayment.class);
    		startActivity(i);
    		
    	}else if(position == 1){
    		
    		showDialog(DIALOG_MANUAL_COMMAND);
    	
    	}else if(position == 2){
    		
    		Intent i = new Intent(this, RegisterCardPayment.class);
    		startActivity(i);
    		
    	}else if(position == 3){
    		
    		Intent i = new Intent(this, RegisterAtm.class);
    		startActivity(i);
    		
    	}
    	
    }
    
    
    public boolean onCreateOptionsMenu(Menu menu){
    	
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.homemenu, menu);
    
    	
    	return true;
    	
    }
    
    

    
    public boolean onOptionsItemSelected(MenuItem item){
		
    	if(item.getItemId() == R.id.launchSettings){
    		
    		Intent intent = new Intent(this, SettingsScreen.class);
    		startActivity(intent);
    	}
    	
    	return true;
    	
    }

   public void disp(String text){
	 Toast.makeText(this, text, 2000).show();
   }
   
   
   
   protected Dialog onCreateDialog(int id){
	   
	   if(id == DIALOG_MANUAL_COMMAND){
		   dialogManualCmd = new Dialog(this);
		   dialogManualCmd.setContentView(R.layout.manualactiondialog);
		   dialogManualCmd.setTitle("Ręczna akcja");
		
		   Button regi = (Button)dialogManualCmd.findViewById(R.id.sendManualAction);
		   regi.setOnClickListener(dialogManualCommand);
		   
		   return dialogManualCmd;
		   
	   
		   
		   
		   
	   }
	   
	   
	   
	   return dialogManualCmd;
	   
	   
	   
   }

   public OnClickListener dialogManualCommand = new OnClickListener(){
	   @Override
	   public void onClick(View v){
		   
		   Editable operacja;
		   EditText txt = (EditText) dialogManualCmd.findViewById(R.id.manualCommandText);
		   operacja = txt.getText();
		   txt.setText("");

		   api.sendCommand(operacja.toString());

		   dismissDialog(DIALOG_MANUAL_COMMAND);
	   }
   };
   

    
}